home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / bc30bug.zip / BUG.CPP next >
C/C++ Source or Header  |  1992-04-28  |  1KB  |  48 lines

  1. /*
  2. * file: bug.c
  3. *
  4. * description:
  5. *    This file demonstrates a nasty BC++ 3.0 initialization bug.
  6. *    Just compile it with the provided project file in the BC IDE.
  7. *    It appears to be immune to any optimizations.
  8. *    ( Be sure to edit the (O)ptions/(D)irectories first! )
  9. *
  10. * By: Gary E. Miller
  11. *    4/28/92
  12. *    gem@cup.portal.com
  13. *    (415)964-1186
  14. */
  15.  
  16. #include <stdio.h>
  17.  
  18. struct ST {
  19.     long stl;
  20.     ST(long a) {stl = a;};
  21. };
  22.  
  23. long stuff()
  24. {
  25.     // st should be initialized ONCE and ONLY ONCE!
  26.     // instead it is initialized every 0x10000 times
  27.     // that this function is called !!!
  28.     static ST st(0);
  29.  
  30.     // should return 0 to LONG_MAX and then wrap to LONG_MIN
  31.     // instead it returns 0 to UINT_MAX and then wraps to 0
  32.     // because the constuctor is called a second time at UINT_MAX!!!
  33.  
  34.     // if you want to know why, just disassemble this function!!
  35.     return(st.stl++);
  36. }
  37.  
  38. int main(int argc, char **argv)
  39. {
  40.     for ( long l = 0 ; l < 100000 ; l++ ) {
  41.         long m = stuff();
  42.         if ( m != l ) {
  43.             printf("BUG!! stuff()=%ld and l=%ld !\n", m ,l);
  44.             return(1);
  45.         }
  46.     }
  47.     return(0);
  48. }